package NewTmas.EditMode;

import NewTmas.FramesandPanels.JobMonitorOpenerFrame;
import taskmaster.auth.User;
import taskmaster.util.LookupLists;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;


public class EditModeJobOpenerFrame extends JFrame implements ActionListener {

    User user;

    JDesktopPane dtp;
    LookupLists ll;
    JList list = null;
        JComboBox    XDim=new JComboBox();
      JComboBox YDim=new JComboBox();

    public EditModeJobOpenerFrame(User u, JDesktopPane dtp, LookupLists ll) {
        this.user = u;
        this.dtp=dtp;
        this.ll=ll;

        createFrame()  ;

    }


    public void createFrame(){

        getContentPane().setBackground(Color.WHITE);


        try {
            list = new JList(ll.getUsersJobs());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Error retrieving user jobs: "+e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }


        getContentPane().setLayout(null);



        for(int x:JobMonitorOpenerFrame.xdim){

            XDim.addItem(x);


        }

        for(int y:JobMonitorOpenerFrame.ydim){

            YDim.addItem(y);


        }

        final JLabel XdimLabel=new JLabel("X Dimension") ;
        final JLabel YdimLabel=new JLabel("Y Dimension");


        XdimLabel.setForeground(Color.BLUE );
        YdimLabel.setForeground(Color.BLUE );

        getContentPane()  .add(XDim);
        getContentPane().add(XdimLabel);
        getContentPane().add(YdimLabel);
        getContentPane().add(YDim);


        XdimLabel .setBounds(380, 440, 96, 20);
        XDim.setBounds(380, 470, 96, 20);
        YdimLabel.setBounds(380, 500, 96, 20);
        YDim.setBounds(380, 530, 96, 20);


        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setBounds(74, 52, 276, 529);
        getContentPane().add(scrollPane);
        setTitle("Select the jobs to edit");
        setSize(500,760);




        JButton btnEdit = new JButton("Edit");
        btnEdit.setBounds(262, 613, 89, 23);
        getContentPane().add(btnEdit);


        JLabel lblJobs = new JLabel("Jobs :");
        lblJobs.setForeground(Color.BLUE);
        lblJobs.setBounds(35, 27, 46, 14);
        getContentPane().add(lblJobs);



        setVisible(true);

        btnEdit.addActionListener(this);




    }

    public void actionPerformed(ActionEvent e) {

           int graphWidth= Integer.parseInt(XDim.getSelectedItem().toString());
                int graphHeight= Integer.parseInt(YDim.getSelectedItem().toString());

        String[] selectedUserJobs = Arrays.copyOf(list.getSelectedValues(), list.getSelectedValues().length, String[].class);

        for(int i=0;i<selectedUserJobs .length;i++)
        {
            EditIntFrame intFrame =new EditIntFrame(selectedUserJobs [i],user,dtp,ll,graphWidth,graphHeight);
        }
        this.setVisible(false);

    }
}
